package common

import (
	"context"
	"git.mycaigou.com/gfyx/common/http_client"
	"git.mycaigou.com/gfyx/common/simplecopier"
	businessOpportunityDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/constants"
)

type commonRepository struct {
}

func NewCommonRepository() CommonReposIface {
	return &commonRepository{}
}

// GetLocation 获取地址列表
func (r *commonRepository) GetLocation(ctx context.Context) ([]map[string]interface{}, error) {
	params := map[string]interface{}{
		"pageNum":  1,
		"pageSize": 999,
	}
	resultData := http_client.BigDataResponse{}
	_, _, errs := http_client.NewBigdataApiHttpClient().Get(ctx, constants.GetLocation, params).EndStruct(&resultData)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return resultData.Data.Rows, nil
}

func (r *commonRepository) FuzzySearchCompanyInfo(ctx context.Context, keyword string) (interface{}, error) {
	params := map[string]interface{}{
		"keyword": keyword,
	}
	resultData := http_client.OpenApiResponse{}
	_, _, errs := http_client.NewOpenApiHttpClient().Post(ctx, constants.FuzzySearch, params, map[string]string{}).EndStruct(&resultData)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return resultData.Data, nil
}

func (r *commonRepository) GetProjectListByDeveloperName(ctx context.Context, req *businessOpportunityDto.GetProjectListByCompanyNameRequest) ([]map[string]interface{}, int, error) {
	bigdataParamsMap, _ := simplecopier.StructToMapByJsonTag(req)
	delete(bigdataParamsMap, "project_name")
	delete(bigdataParamsMap, "company_type")
	if req.ProjectName != "" {
		bigdataParamsMap["project_title_like"] = "%" + req.ProjectName + "%"
	}
	resultData := http_client.BigDataResponse{}
	_, _, errs := http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetDeveloperProjectList, bigdataParamsMap, map[string]string{}).EndStruct(&resultData)
	if len(errs) > 0 {
		return nil, 0, errs[0]
	}
	return resultData.Data.Rows, resultData.Data.TotalNum, nil
}

func (r *commonRepository) GetProjectListBySupplierName(ctx context.Context, req *businessOpportunityDto.GetProjectListByCompanyNameRequest) ([]map[string]interface{}, int, error) {
	bigdataParamsMap, _ := simplecopier.StructToMapByJsonTag(req)
	bigdataParamsMap["supplier_name"] = req.CompanyName
	delete(bigdataParamsMap, "project_name")
	delete(bigdataParamsMap, "company_type")
	delete(bigdataParamsMap, "company_name")
	if req.ProjectName != "" {
		bigdataParamsMap["coop_prj_name_like"] = "%" + req.ProjectName + "%"
	}
	resultData := http_client.BigDataResponse{}
	_, _, errs := http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetSupplierProjectList, bigdataParamsMap, map[string]string{}).EndStruct(&resultData)
	if len(errs) > 0 {
		return nil, 0, errs[0]
	}
	return resultData.Data.Rows, resultData.Data.TotalNum, nil
}
